package com.du;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Practice {
    static int trueItem = 0; // 正确题目数
    static int totalItem = 0; // 总题目数
    static List list = new ArrayList(); // 放入答题情况

    public static void main(String[] args) {
        try {
            loop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loop() throws Exception {
        System.out.println("数学计算能力训练系统");
        System.out.println("");
        System.out.println("学号: 1806410612    姓名: 杜纪雄");
        System.out.println("");
        System.out.println("1 -- 加法     2 -- 减法    3 -- 乘法   4 -- 除法   0 -- 退出");
        System.out.println("");

        System.out.println("清输入训练的项目: ");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        switch (i) {
            case 1: {
                long before = System.currentTimeMillis();
                System.out.println("训练加法");
                add(before); // 加法
                break;
            }
            case 2: {
                long before = System.currentTimeMillis();
                System.out.println("训练减法");
                sub(before); // 减法
                break;
            }
            case 3: {
                long before = System.currentTimeMillis();
                System.out.println("训练乘法法");
                mul(before); // 乘法
                break;
            }
            case 4: {
                long before = System.currentTimeMillis();
                System.out.println("训练乘法法");
                div(before); // 除法
                break;
            }
            case 0: {
                System.out.println();
                System.out.println("退出");
                break;
            }
            default: {
                throw new Exception("你的输入有误, 请重新运行!");
            }
        }
    }

    private static void add(Long before) throws Exception {
        int x = (int)(Math.random() * 50); // 得到第一个数
        int y = (int)(Math.random() * 50); // 得到第二个数
        System.out.println(x + " + " + y + " = ?  请答题");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        if (i == -1) {
            soutProcess(list, before);
            loop();
            return;
        }
        totalItem++;  // 总题数增加
        String process = x + "+" + y + "=" + i;
        boolean is_True = (i == x + y);
        String trueInfo = process + " " + is_True;
        list.add(trueInfo);
        if (i == x + y) {
            System.out.println("回答正确! 你真棒");
            trueItem++; // 正确数增加
        } else {
            System.out.println("这也能错? 笨比");
        }
        System.out.println();
        System.out.println("再来一题");
        add(before); // 使用递归
    }

    private static void sub(Long before) throws Exception {
        int x = (int)(Math.random() * 50);
        int y = (int)(Math.random() * 50);
        System.out.println(x + " - " + y + " = ?  请答题");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        if (i == -1) {
            soutProcess(list, before);
            loop();
            return;
        }
        totalItem++;
        String process = x + "-" + y + "=" + i;
        boolean is_True = (i == x - y);
        String trueInfo = process + " " + is_True;
        list.add(trueInfo);
        if (is_True) {
            System.out.println("回答正确! 你真棒");
            trueItem++;
        } else {
            System.out.println("这也能错? 笨比");
        }
        System.out.println("再来一题");
        sub(before);
    }

    private static void mul(Long before) throws Exception {
        int x = (int)(Math.random() * 50);
        int y = (int)(Math.random() * 50);
        System.out.println(x + " * " + y + " = ?  请答题");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        if (i == -1) {
            soutProcess(list, before);
            loop();
            return;
        }
        totalItem++;
        String process = x + "*" + y + "=" + i;
        boolean is_True = (i == x + y);
        String trueInfo = process + " " + is_True;
        if (i == x * y) {
            System.out.println("回答正确! 你真棒");
            trueItem++;
        } else {
            System.out.println("这也能错? 笨比");
        }
        System.out.println("再来一题");
        mul(before);
    }

    private static void div(Long before) throws Exception {
        int x = (int)(Math.random() * 50);
        int y = (int)(Math.random() * 50);
        System.out.println(x + " / " + y + " = ?  请答题");
        Scanner scan = new Scanner(System.in);
        double i = scan.nextDouble();
        if (i == -1 ) {
            soutProcess(list, before);
            loop();
            return;
        }
        totalItem++;
        String process = x + "/" + y + "=" + i;
        boolean is_True = (i == x + y);
        String trueInfo = process + " " + is_True;
        if (i == x / y) {
            System.out.println("回答正确! 你真棒");
            trueItem++;
        } else {
            System.out.println("这也能错? 笨比");
        }
        System.out.println("再来一题");
        div(before);
    }

    private static void soutProcess(List list, Long before) {
        System.out.println("---下列公布成绩---");
        for (Object o : list) {
            System.out.println(o);
        }
        System.out.println("总题数为: " + totalItem);
        System.out.println("做对的题目数为: " + trueItem);
        System.out.println("正确率为: "+ ((double)trueItem/(double)totalItem * 100) + "%");
        long after = System.currentTimeMillis();
        System.out.println("总耗时" + (after - before)/1000 + "s");
        System.out.println();
        init(); // 归零
    }

    private static void init() {
        totalItem = 0;
        trueItem = 0;
        list = new ArrayList();
    }



}
